package com.woniu.map.service.impl;

import com.woniu.internalcommon.constant.CommonStatusEnum;
import com.woniu.internalcommon.dto.ResponseResult;
import com.woniu.internalcommon.response.TerminalResponse;
import com.woniu.map.remote.TerminalClient;
import com.woniu.map.service.TerminalService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class TerminalServiceImpl implements TerminalService {

    @Resource
    private TerminalClient terminalClient;

    /**
     *  添加终端
     * @param name
     * @param desc
     * @return
     */
    public ResponseResult<TerminalResponse> addTerminal(String name, String desc)
    {
        try {
            TerminalResponse terminalResponse = terminalClient.addTerminal(name, desc);
            return ResponseResult.success(terminalResponse);
        }catch (Exception e){
            return ResponseResult.fail(e.getMessage())
                    .setCode(CommonStatusEnum.MAP_DISTRICT_ERROR.getCode());
        }
    }


    @Override
    public ResponseResult<TerminalResponse> aroundSearch(String center, Integer radius) {
        try {
            List<TerminalResponse> terminalResponses = terminalClient.aroundSearch(center, radius);

           List<TerminalResponse> terminal = new ArrayList<>();
            for (TerminalResponse terminalRespons : terminalResponses) {
                if (terminalRespons.getCarId() != null)
                    terminal.add(terminalRespons);
            }
            return ResponseResult.success(terminal);
        }catch (Exception e){
            return ResponseResult.fail(e.getMessage()).
                    setCode(CommonStatusEnum.MAP_DISTRICT_ERROR.getCode());
        }

    }

    /**
     * 查询终端列表
     * @param page 默认 为 1
     * @return
     */
    @Override
    public ResponseResult<TerminalResponse> getTerminal(String page) {
        try {
            return ResponseResult.success(terminalClient.getTerminal("", "", page));
        }catch (Exception e){
            return ResponseResult.fail(e.getMessage()).setCode(20000);
        }
    }
}
